home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility1 / gs261src.zip / GSIMAGE2.C < prev    next >
C/C++ Source or Header  |  1993-05-13  |  8KB  |  261 lines

  1. /* Copyright (C) 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gsimage2.c */
  20. /* Additional image procedures for Ghostscript library */
  21. /* This file is logically part of gsimage.c; we have split it out */
  22. /* to reduce the code working set. */
  23. #include "gx.h"
  24. #include "memory_.h"
  25. #include "gpcheck.h"
  26. #include "gserrors.h"
  27. #include "gxfixed.h"
  28. #include "gxarith.h"
  29. #include "gxmatrix.h"
  30. #include "gscspace.h"
  31. #include "gspaint.h"
  32. #include "gzstate.h"
  33. #include "gzdevice.h"            /* requires gsstate.h */
  34. #include "gzcolor.h"            /* requires gxdevice.h */
  35. #include "gzpath.h"
  36. #include "gxcolor.h"
  37. #include "gxcpath.h"
  38. #include "gxdevmem.h"
  39. #include "gximage.h"
  40.  
  41. /* ------ Unpacking procedures ------ */
  42.  
  43. void
  44. image_unpack_1_spread(const gs_image_enum *penum, const sample_map *pmap,
  45.   register byte *bufp, register const byte *data, uint dsize, uint inpos)
  46. {    register int spread = penum->spread;
  47.     int left = dsize;
  48.     register const byte *map = &pmap->table.lookup8[0];
  49.     while ( left-- )
  50.        {    register uint b = *data++;
  51.         *bufp = map[b >> 7]; bufp += spread;
  52.         *bufp = map[(b >> 6) & 1]; bufp += spread;
  53.         *bufp = map[(b >> 5) & 1]; bufp += spread;
  54.         *bufp = map[(b >> 4) & 1]; bufp += spread;
  55.         *bufp = map[(b >> 3) & 1]; bufp += spread;
  56.         *bufp = map[(b >> 2) & 1]; bufp += spread;
  57.         *bufp = map[(b >> 1) & 1]; bufp += spread;
  58.         *bufp = map[b & 1]; bufp += spread;
  59.        }
  60. }
  61.  
  62. void
  63. image_unpack_2_spread(const gs_image_enum *penum, const sample_map *pmap,
  64.   register byte *bufp, register const byte *data, uint dsize, uint inpos)
  65. {    register int spread = penum->spread;
  66.     int left = dsize;
  67.     register const byte *map = &pmap->table.lookup8[0];
  68.     while ( left-- )
  69.        {    register unsigned b = *data++;
  70.         *bufp = map[b >> 6]; bufp += spread;
  71.         *bufp = map[(b >> 4) & 3]; bufp += spread;
  72.         *bufp = map[(b >> 2) & 3]; bufp += spread;
  73.         *bufp = map[b & 3]; bufp += spread;
  74.        }
  75. }
  76.  
  77. void
  78. image_unpack_8_spread(const gs_image_enum *penum, const sample_map *pmap,
  79.   register byte *bufp, register const byte *data, uint dsize, uint inpos)
  80. {    register int spread = penum->spread;
  81.     register int left = dsize;
  82.     register const byte *map = &pmap->table.lookup8[0];
  83.     while ( left-- )
  84.        {    *bufp = map[*data++]; bufp += spread;
  85.        }
  86. }
  87.  
  88. void
  89. image_unpack_12(const gs_image_enum *penum, const sample_map *pmap,
  90.   register byte *bufp, register const byte *data, uint dsize, uint inpos)
  91. {    register int spread = penum->spread;
  92.     register int left = dsize;
  93.     register const byte *map = &pmap->table.lookup8[0];
  94.     /* We have to deal with the 3 cases of inpos % 3 separately. */
  95.     /* (In fact, this is the only reason inpos is passed to */
  96.     /* the unpacking procedures at all.) */
  97.     /****** DOESN'T DO MAPPING RIGHT. ******/
  98.     /* Let N = inpos / 3. */
  99.     switch ( inpos % 3 )
  100.        {
  101.     case 1:
  102.         /* bufp points to byte 2N, which was already filled */
  103.         /* with the leftover byte from the previous call. */
  104.         bufp += spread;
  105.         *bufp = *data++ << 4;
  106.         if ( !--left ) return;
  107.     case 2:
  108.         /* bufp points to byte 2N+1, which was half-filled */
  109.         /* with the second leftover byte from the previous call. */
  110.         *bufp = map[*bufp + (*data++ >> 4)];
  111.         --left;
  112.     case 0:
  113.         /* Nothing special to do. */
  114.         ;
  115.        }
  116.     /* Just drop the low 4 bits of each 12. */
  117.     while ( left >= 3 )
  118.        {    *bufp = map[*data];
  119.         bufp += spread;
  120.         *bufp = map[(data[1] << 4) + (data[2] >> 4)];
  121.         bufp += spread;
  122.         data += 3;
  123.        }
  124.     switch ( left )
  125.        {
  126.     case 2:                /* dddddddd xxxxdddd */
  127.         bufp[1] = data[1] << 4;
  128.     case 1:                /* dddddddd */
  129.         *bufp = map[*data];
  130.     case 0:                /* Nothing more to do. */
  131.         ;
  132.        }
  133. }
  134.  
  135. /* ------ Rendering procedures ------ */
  136.  
  137. /* Rendering procedure for handling color images. */
  138. typedef union {
  139.     byte v[4];
  140.     ulong all;
  141. } color_samples;
  142. int
  143. image_render_color(gs_image_enum *penum, byte *buffer, uint w, int h)
  144. {    gs_state *pgs = penum->pgs;
  145.     fixed    dxx = penum->fxx, dxy = penum->fxy,
  146.         dyx = penum->fyx, dyy = penum->fyy;
  147.     int skew = penum->skewed;
  148.     fixed xt = penum->xcur;
  149.     fixed ytf = penum->ycur;
  150.     int yt = penum->yci, iht = penum->hci;
  151.     gs_color_space *pcs = pgs->color_space;
  152.     cs_proc_remap_color((*remap_color)) = pcs->type->remap_color;
  153.     gs_client_color cc;
  154.     int device_color = penum->device_color;
  155.     cmap_proc_rgb((*map_rgb)) = pgs->cmap_procs->map_rgb;
  156.     cmap_proc_cmyk((*map_cmyk)) = pgs->cmap_procs->map_cmyk;
  157.     gx_device_color devc1, devc2;
  158.     gx_device_color _ss *spdevc = &devc1;
  159.     gx_device_color _ss *spdevc_next = &devc2;
  160. #define pdevc ((gx_device_color *)spdevc)
  161. #define pdevc_next ((gx_device_color *)spdevc_next)
  162.     int spp = penum->spp;
  163.     int i;
  164.     fixed xl = xt;
  165.     const byte *psrc = buffer;
  166.     fixed xrun = xt;        /* x at start of run */
  167.     int irun = fixed2int_var_rounded(xrun);    /* int xrun */
  168.     fixed yrun = ytf;        /* y ditto */
  169.     color_samples run;        /* run value */
  170.     color_samples next;        /* next sample value */
  171.     byte *bufend = buffer + w;
  172.     bufend[0] = ~bufend[-spp];    /* force end of run */
  173.     if_debug5('b', "[b]y=%d w=%d xt=%f yt=%f yb=%f\n",
  174.           penum->y, w,
  175.           fixed2float(xt), fixed2float(ytf), fixed2float(ytf + dyy));
  176.     run.all = 0;
  177.     next.all = 0;
  178.     for ( i = 0; i < spp; i++ )
  179.         cc.paint.values[i] = penum->map[i].decode_base;
  180.     (*remap_color)(&cc, pcs, pdevc, pgs);
  181.     while ( psrc <= bufend )    /* 1 extra iteration */
  182.                 /* to handle final run */
  183.     {    next.v[0] = psrc[0];
  184.         next.v[1] = psrc[1];
  185.         next.v[2] = psrc[2];
  186.         if ( spp == 4 )        /* cmyk */
  187.         {    next.v[3] = psrc[3];
  188.             psrc += 4;
  189.             if ( next.all == run.all ) goto inc;
  190.             if ( device_color )
  191.             {    (*map_cmyk)(byte2frac(next.v[0]),
  192.                     byte2frac(next.v[1]),
  193.                     byte2frac(next.v[2]),
  194.                     byte2frac(next.v[3]),
  195.                     pdevc_next, pgs);
  196.                 goto f;
  197.             }
  198.             else
  199.             {    decode_sample(next.v[3], cc, 3);
  200.             }
  201.         }
  202.         else            /* rgb */
  203.         {    psrc += 3;
  204.             if ( next.all == run.all ) goto inc;
  205.             if ( device_color )
  206.             {    (*map_rgb)(byte2frac(next.v[0]),
  207.                     byte2frac(next.v[1]),
  208.                     byte2frac(next.v[2]),
  209.                     pdevc_next, pgs);
  210.                 goto f;
  211.             }
  212.         }
  213.         decode_sample(next.v[0], cc, 0);
  214.         decode_sample(next.v[1], cc, 1);
  215.         decode_sample(next.v[2], cc, 2);
  216.         (*remap_color)(&cc, pcs, pdevc_next, pgs);
  217. f:        if_debug7('B', "[B]0x%x,0x%x,0x%x,0x%x -> %ld,%ld,%d\n",
  218.             next.v[0], next.v[1], next.v[2], next.v[3],
  219.             pdevc_next->color1, pdevc_next->color2,
  220.             pdevc_next->halftone_level);
  221.         /* Even though the supplied colors don't match, */
  222.         /* the device colors might. */
  223.         if ( devc1.color1 != devc2.color1 ||
  224.              devc1.halftone_level != devc2.halftone_level ||
  225.              (devc1.halftone_level &&
  226.               devc1.color2 != devc2.color2) ||
  227.              psrc > bufend    /* force end of last run */
  228.            )
  229.         {    /* Fill the region between */
  230.             /* xrun/irun and xl */
  231.             gx_device_color _ss *sptemp;
  232.             int code;
  233.             if ( skew )
  234.             {    /* Parallelogram */
  235.                 code = gz_fill_pgram_fixed(xrun, yrun,
  236.                     xl - xrun, ytf - yrun, dyx, dyy,
  237.                     pdevc, pgs);
  238.                 xrun = xl;
  239.                 yrun = ytf;
  240.             }
  241.                 else
  242.             {    /* Rectangle */
  243.                 int xi = irun;
  244.                 int wi = (irun = fixed2int_var_rounded(xl)) - xi;
  245.                 if ( wi < 0 ) xi += wi, wi = -wi;
  246.                 code = gz_fill_rectangle(xi, yt, wi, iht, pdevc, pgs);
  247.             }
  248.             if ( code < 0 )
  249.                 return code;
  250.             return_if_interrupt();
  251.             sptemp = spdevc;
  252.             spdevc = spdevc_next;
  253.             spdevc_next = sptemp;
  254.         }
  255.         run.all = next.all;
  256. inc:        xl += dxx;
  257.         ytf += dxy;        /* harmless if no skew */
  258.     }
  259.     return 1;
  260. }
  261.